{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows how to compare product prices using AgentQL with the [Google Colaboratory](https://colab.research.google.com/) environment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install [AgentQL](https://pypi.org/project/agentql/) library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install agentql"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install the Playwright dependency required by AgentQL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!playwright install chromium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can [store](https://medium.com/@parthdasawant/how-to-use-secrets-in-google-colab-450c38e3ec75) keys in Google Colab's secrets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from google.colab import userdata\n",
    "\n",
    "os.environ[\"AGENTQL_API_KEY\"]=userdata.get('AGENTQL_API_KEY')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run AgentQL script. Please note that an online environment like Google Colab supports **asynchronous version** of AgentQL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import agentql\n",
    "from playwright.async_api import async_playwright\n",
    "from IPython.display import HTML\n",
    "from base64 import b64encode\n",
    "\n",
    "# Set the URL to the desired website\n",
    "BESTBUY_URL = \"https://www.bestbuy.com/site/nintendo-switch-oled-model-w-joy-con-white/6470923.p?skuId=6470923\"\n",
    "TARGET_URL = \"https://www.target.com/p/nintendo-switch-oled-model-with-white-joy-con/-/A-83887639#lnk=sametab\"\n",
    "NINTENDO_URL = \"https://www.nintendo.com/us/store/products/nintendo-switch-oled-model-white-set/\"\n",
    "\n",
    "# Define the queries to get the product price\n",
    "PRODUCT_INFO_QUERY = \"\"\"\n",
    "{\n",
    "    nintendo_switch_price(integer)\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "async with async_playwright() as playwright, await playwright.chromium.launch() as browser:\n",
    "\n",
    "      # Set up the video recording\n",
    "      video_dir = os.path.abspath(\"videos\")\n",
    "      context = await browser.new_context(\n",
    "        record_video_dir=\"videos/\",\n",
    "        record_video_size={\"width\": 1280, \"height\": 720}\n",
    "        )\n",
    "      page = await agentql.wrap_async(await context.new_page())\n",
    "      await page.goto(BESTBUY_URL, timeout= 60000)\n",
    "\n",
    "      # Use query_data() method to fetch the price from the BestBuy page\n",
    "      response = await page.query_data(PRODUCT_INFO_QUERY)\n",
    "\n",
    "      print(\"Price at BestBuy: \", response[\"nintendo_switch_price\"])\n",
    "\n",
    "      await page.goto(NINTENDO_URL, timeout= 60000)\n",
    "\n",
    "      # Use query_data() method to fetch the price from the Nintendo page\n",
    "      response = await page.query_data(PRODUCT_INFO_QUERY)\n",
    "\n",
    "      print(\"Price at Nintendo: \", response[\"nintendo_switch_price\"])\n",
    "\n",
    "      await page.goto(TARGET_URL, timeout= 60000)\n",
    "\n",
    "      # Use query_data() method to fetch the price from the Target page\n",
    "      response = await page.query_data(PRODUCT_INFO_QUERY)\n",
    "\n",
    "      print(\"Price at Target: \", response[\"nintendo_switch_price\"])\n",
    "\n",
    "      # Display the video\n",
    "      video_files = [f for f in os.listdir(video_dir) if f.endswith('.webm')]\n",
    "      if video_files:\n",
    "          video_path = os.path.join(video_dir, video_files[0])\n",
    "          with open(video_path, 'rb') as f:\n",
    "            video_bytes = f.read()\n",
    "            \n",
    "          video_b64 = b64encode(video_bytes).decode('utf-8')\n",
    "          video_html = f\"\"\"\n",
    "          <video width=\"800\" controls>\n",
    "            <source src=\"data:video/webm;base64,{video_b64}\" type=\"video/webm\">\n",
    "            Your browser does not support the video tag.\n",
    "          </video>\n",
    "          \"\"\"\n",
    "          display(HTML(video_html))\n",
    "      else:\n",
    "          print(\"No video file was created\") "
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
